//
//  WFChannelListTableViewCell.m
//  FAKER
//
//  Created by Jianwei on 15/11/6.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFChannelListTableViewCell.h"
#import "AllHeader.h"
#import "FitHeader.h"

@implementation WFChannelListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _dataArray = [NSArray array];
        _manager = [CoreDataManager shareManager];
        [self createCell];
    }
    return self;
}
- (void)createCell
{
    //    _plusImageView = [[BaseClassImageView alloc]initWithFrame:CGRectMake(340 * FITWIDTH, 20, 20, 20)];
    //    [CONTENT_VIEW addSubview:_plusImageView];
    
    _titleLabel = [[BaseClassLabel alloc]init];
    _titleLabel.font = [UIFont fontWithBold:1 IPhone6sPlus:15 Iphone6s:15 Iphone5s:15 Iphone4s:15];
    [CONTENT_VIEW addSubview:_titleLabel];
    
    _button = [UIButton buttonWithType:1];
    //    [image imageWithColor:[UIColor grayColor]];
    _button.tintColor = [UIColor grayColor];
    
    
    //    _button.backgroundColor = [UIColor grayColor];
    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [CONTENT_VIEW addSubview:_button];
    
}
- (void)buttonAction
{
    
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"NewChannel"];
    _dataArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    for (NewChannel *newChannel in _dataArray) {
        if ([newChannel.api_url isEqualToString:_model.api_url]) {
            [_manager.managedObjectContext deleteObject:newChannel];
            [_manager saveContext];
            [_button setImage:[UIImage imageNamed:@"common_icon_subscribe@3x"] forState:0];
            return;
        }
    }
    NewChannel *newChannel = [NSEntityDescription insertNewObjectForEntityForName:@"NewChannel" inManagedObjectContext:_manager.managedObjectContext];
    newChannel.title = _model.title;
    newChannel.api_url = _model.api_url;
    newChannel.pic = _model.pic;
    newChannel.block_color = _model.block_color;
    [_manager saveContext];
    [_button setImage:[UIImage imageNamed:@"style_lite_selected@2x"] forState:0];
    return;
    
}
- (void)setModel:(WFSecondChannelListModel *)model
{
    _model = model;
    
    _titleLabel.text = _model.title;
    
    _titleLabel.frame = CGRectMake(20, 20, 200, 20);
    
    [self performSelector:@selector(fit) withObject:nil afterDelay:0];
    
    
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"NewChannel"];
    _dataArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    for (NewChannel *newChannel in _dataArray) {
        if ([newChannel.api_url isEqualToString:_model.api_url]) {
            [_button setImage:[UIImage imageNamed:@"style_lite_selected@2x"] forState:0];
            return;
        }
    }
    [_button setImage:[UIImage imageNamed:@"common_icon_subscribe@3x"] forState:0];
}
- (void)fit
{
    [_titleLabel sizeToFit];
    
    //    _iimageView.frame = CGRectMake(_titleLabel.X + _titleLabel.WIDTH, _titleLabel.Y, 20, 20);
    
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    //    _plusImageView.image = [UIImage imageNamed:@"common_icon_subscribe@3x"];
    //    _titleLabel.frame = CGRectMake(20 * FITWIDTH, 20, [Adaptive AdaptiveWidthByString:_model.title Height:20 FontSize:_titleLabel.font.pointSize], 20);
    _button.frame = CGRectMake(340 * FITWIDTH, 20, 20, 20);
    if (NSUSERDEFAULTS_NIGHT) {
        _titleLabel.textColor = [UIColor grayColor];
    }
    
}
@end
